<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>

</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row">
                <form class="layui-form" lay-filter="myform" id="myform">
                	<input type="reset" id="btReset" class="layui-hide" >
                    <div class="layui-form-item">
                        <label class="layui-form-label">名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" id="name"  autocomplete="off" class="layui-input">
                        </div>
                        <label class="layui-form-label">科目</label>
                        <div class="layui-input-inline">
                            <input type="text" id="categoryName" name="categoryName" autocomplete="off" class="layui-input" select>
                            <input type="hidden" id="categoryId" name="categoryId">
                        </div>
                        <label class="layui-form-label">学期学年</label>
                        <div class="layui-input-inline">
                            <select name="gradeYear" id="gradeYear"></select>
                        </div>

                    </div>
                    <div class="layui-form-item" style="text-align: center">
                        <div class="layui-inline">
                            <div class="layui-btn-group">
                                <button type="button" class="layui-btn layui-btn-primary" data-type="query"><i class="layui-icon">&#xe615;</i>查询</button>
                                <button type="button" class="layui-btn layui-btn-primary" data-type="resetBtn"><i class="layui-icon">&#xe669;</i>重置</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="row">
                <table class="layui-hide"  id="main-table" lay-filter="main-table"></table>
            </div>
        </div>
    </div>
</div>

<script th:inline="none"></script>
<script>
let userId = `[[${session.me.id}]]`;
let roleCode = (`[[${session.me.roleCode}]]`);
layui.link('layuiadmin/plugins/layui_complete-master/autocomplete.css');
layui.extend({
    autocomplete: '{/}layuiadmin/plugins/layui_complete-master/autocomplete'
});
    layui.use(['table','form','autocomplete'], function(){
        var table = layui.table,
            autocomplete = layui.autocomplete,
            form = layui.form;
        select('gradeYear/getPageList?limit=30&page=1' , 'gradeYear',form,'name','id')
        setAutocomplete('libCategory/getPageList?page=1&limit=20',autocomplete,"categoryName",'{{d.name}}','{{d.name}}',"setMajor");
        window.setMajor = function(data){
            $("#categoryId").val(data.id);
            $("#categoryName").val(data.name);
        }
        table.render({
            elem: '#main-table'
            ,height: (top.height-85)+'px'
            ,url: 'paper/getPageList'
            ,title: '试卷列表'
            ,page: true 
            ,toolbar: '#toolbarPrice' 
            ,cols: [
                [ 
                {type: 'checkbox'}
                ,{type: 'numbers'}
                ,{field: 'name', title: "试卷名称",width:220,templet:function(d){
                	if(roleCode.indexOf("admin")==-1){
                		return d.name;
                	}else{
                		return "<a class='colum-link' href='javascript:void(0)' onclick='viewPaper(\""+d.id+"\")'>" + d.name + "</a>";
                	}

                }}
                ,{field: 'code',title: "编号",width:90}
                ,{field: 'majorName',title: "考试专业",width:120}
                ,{field: 'categoryName',title: "考试科目",width:150}

                ,{field: 'studentTypeTxt',title: "学生类型",width:90}
                ,{field: 'paperTypeTxt',title: "A/B/C卷",width:90}
                ,{field: 'testPhaseTxt',title: "考试阶段",width:90}
                ,{field: 'testTypeTxt',title: "考试方式",width:90}
                ,{field: 'lgTxt',title: "试卷语言",width:90}
                ,{field: 'stateTxt',title: "记录状态",width:90}
                ,{field: 'howlong',title: "考试时长",width:90}
                ,{field: 'gradeName',title: "考生年级",width:90}
                ,{field: 'majorName',title: "学年学期",width:180,templet:function (d){
                        return d.gradeYearName + d.semesterTxt;
                    }}
                 ,{field: 'score1',title: "每道单选分",width:100}
                 ,{field: 'score2',title: "每道多选分",width:100}
                 ,{field: 'score3',title: "每道判断分",width:100}
                 ,{field: 'score4',title: "每道简单分",width:100}
                 ,{field: 'score5',title: "每道计算分析分",width:120}
                ,{field: 'createTime', title: "登记时间",width:170}
            ]
            ]
            ,done:function(){
            	if(roleCode.indexOf("admin")==-1){
            		$("button[lay-event!=mock]").hide();
            	}
          	  enableRowClick(table,form,'main-table',"main-table");
             }
        });
        table.on('edit(main-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
        });
        //头工具栏事件
        table.on('toolbar(main-table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id); 
            switch(obj.event){
                case 'add':
                    top.layer.open({
                        id:"joinpaper",
                        type: 2,
                        title:'组卷',
                        area:['1300px','850px'],
                        content:  'rooter?url=ksxt/paper/add-paper',
                        btn:['组卷','关闭'],
                        yes:function(index,layero){
                            var dataForm = top.layer.getChildFrame('form', index);
                            dataForm.find("#submitBtn").click();

                            return false
                        }
                    });
                    break;
                case 'add2':
                    top.layer.open({
                        id:"joinpaper2",
                        type: 2,
                        title:'组卷',
                        area:['1600px','900px'],
                        content:  'rooter?url=ksxt/paper/add-paper2',
                        btn:['关闭']
                    });
                    break;
                case 'edit2':
                    var data = checkStatus.data;
                    if(data.length===0){
                        layer.msg("请选择试卷!")
                        return false;
                    }
                    top.layer.open({
                        id:"joinpaper2",
                        type: 2,
                        title:'组卷',
                        area:['1600px','900px'],
                        content:  'rooter?url=ksxt/paper/add-paper2&joinid='+data[0].id,
                        btn:['关闭']
                    });
                    break;
                case 'confirm':
                    var data = checkStatus.data;
                    if(data.length===0){
                        layer.msg("请选择试卷!")
                        return false;
                    }
                    var ids=[],values = {};
                    $.each(data,function(i,v){
                        ids.push(v.id);
                    })
                    values['ids'] = ids.join(",");
                    layer.confirm("确认后不可修改，继续该操作吗？",function (index,layero){
                        axios.post("paper/confirm",values).then(res=>{
                            if(res.data.code==0){
                                table.reload("main-table");
                                layer.close(index)
                            }
                            layer.msg(res.data.msg)

                        })
                    })

                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if(data.length>0){
                        var values = {};
                        var ids=[];
                        $.each(data,function(i,v){
                            ids.push(v.id);
                        })
                        values['ids'] = ids;
                        layer.confirm('确定要删除当前行么?', {icon: 3, title:'提示'}, function(index){
                        	$.post("paper/delete",values,function(res){
                        		if (res.code == 1) {
                                    layer.msg(res.msg);
                                } else {
                                    layer.close(index);
                                    table.reload('main-table');
                                    layer.msg("删除成功。");
                                }
                        	})
                        });
                    }else{
                        layer.msg("请选择操作数据!")
                    }
                    break;
                case 'edit':
                    var data2 = checkStatus.data;
                    if(data2.length>0){
                        edit(data2[0].id);
                    }else{
                        layer.msg("请选择操作数据！")
                    }
                    break;
                case 'viewQuestion':
                    var data = checkStatus.data;
                    if(data.length===0){
                        return layer.msg("请选择记录")
                    }
                    queryQuestion(data[0].id);
                    break;
                case 'word':
                    var data = checkStatus.data;
                    if(data.length===0){
                        return layer.msg("请选择试卷")
                    }
                    $("#hideForm2").empty().append($("<input type='text' name='paperId'>").val(data[0].id+""));
                    $("#hideForm2").attr("action","wordPaper/export").submit();
                    break;
                case 'word2':
                    var data = checkStatus.data;
                    if(data.length===0){
                        return layer.msg("请选择试卷")
                    }
                    $("#hideForm2").empty().append($("<input type='text' name='paperId'>").val(data[0].id+"")).append($("<input type='text' name='hasAnswer'>").val(1));
                    $("#hideForm2").attr("action","wordPaper/export").submit();
                    break;
            };
        });
        var $ = layui.$, active = {
            query: function(){

                table.reload("main-table",{where:{name:$("#name").val(),gradeYear:$("#gradeYear").val()
                        ,difficulty:$("#difficulty").val(),categoryId:$("#categoryId").val()
                }})
            },
            resetBtn:function () {
                $("#btReset").click();
                
            }
        };
        window.loadGrid = function(tableName){
            table.reload(tableName);
        }
        window.closeWin = function (name){
            top.layer.closeAll();
        }
        $('.layui-form .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
    window.queryQuestion=function (id){
        layer.open({
            id:"openwin",
            type: 2,
            title:'查看试题',
            area:['1500px','750px'],
            content:  "rooter?url=ksxt/paper/question-list&paperId="+id,
            btn:['关闭']
        });
        //window.location.href = "rooter?url=ksxt/paper/question-list&paperId="+id
    }
    function edit(id){
    	layer.open({
            id:"openwin",
            type: 2,
            title:'修改试卷信息',
            area:['1100px','750px'],
            content:  'rooter?url=ksxt/paper/edit-paper&id='+id,
            btn:['保存','关闭'],
            yes:function(index,layero){
                var dataForm = layer.getChildFrame('form', index);
                dataForm.find("#submitBtn").click();
                return false
            }
        });
    }
    function viewPaper(paperid){
        queryQuestion(paperid);
    }

</script>
<script type="text/html" id="toolbarPrice">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add2"><i class="layui-icon layui-icon-add-circle"></i>新建组卷</button>
        <button class="layui-btn layui-btn-sm" lay-event="edit2"><i class="layui-icon layui-icon-add-circle"></i>修改组卷</button>
    <!--    <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-addition"></i>自动组卷</button>-->
        <button class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</button>
        <button class="layui-btn layui-btn-sm" lay-event="confirm"><i class="layui-icon">&#x1005;</i>确认</button>
        <button class="layui-btn layui-btn-sm" lay-event="viewQuestion"><i class="layui-icon layui-icon-layer"></i>查看试题</button>
        <button class="layui-btn layui-btn-sm" lay-event="word"><i class="layui-icon layui-icon-file"></i>导出</button>
        <button class="layui-btn layui-btn-sm" lay-event="word2"><i class="layui-icon layui-icon-file"></i>导出2</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>
    </div>
</script>
</body>
</html>